import {isObject, isReservedTag} from "../utils";

function vnode(vm, text, tag, data = {}, children,componentInstance) {
  return {
    vm,
    text,
    tag,
    data,
    children,
    key : data.key,
    componentInstance
  }
}
function createComponent(vm,tag,data,children,Ctor){
  // 获取父类构造函数t
  const baseCtor = vm.$options._base;
  if(isObject(Ctor)){
    Ctor = baseCtor.extend(Ctor);
  }
  data.hook = { // 组件的生命周期钩子
    init(vnode){
      let child = vnode.componentInstance = new Ctor({});
      child.$mounted() // 组件的挂载
    }
  }
  console.log(Ctor)

  return vnode(vm,undefined,`vue-component-${Ctor.cid}-${tag}`,data,undefined,{Ctor,children});
}
export function createElement(vm, tag, data = {}, children) {
  if(!isReservedTag(tag)){
    // 如果是组件需要拿到组件的定义,通过组件的定义创造虚拟节点
    let Ctor = vm.$options.components[tag];
    return createComponent(vm,tag,data,children,Ctor)
  }

  return vnode(vm, undefined, tag, data, children)
}
export function createText(vm, text) {
  return vnode(vm, text)
}
